﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace TestWinApi
{
    public class FileSearch
    {
        private Dictionary<long, string> _data = new Dictionary<long, string>();
        private Dictionary<char, Dictionary<long, int>> charIndexs = new Dictionary<char, Dictionary<long, int>>();
        public void Init(IEnumerable<string> datas)
        {
            int i = 0;
            foreach (var item in datas)
            {
                int id = i++;
                _data[id] = item;
                InsertIndex(id, item);
            }
            //var ls = charIndexs.Keys.ToArray();
            //foreach (var item in ls)
            //{
            //    var vl = charIndexs[item];
            //    charIndexs[item] = (from q in vl orderby q.Index ascending select q).ToList();

            //}
        }
        private void InsertIndex(long id, string data)
        {
            for (int i = 0; i < data.Length; i++)
            {
                char ch = data[i];
                Dictionary<long, int> chs;
                if (charIndexs.ContainsKey(ch))
                {
                    chs = charIndexs[ch];
                }
                else
                {
                    chs = new Dictionary<long, int>();
                    charIndexs[ch] = chs;
                }
                chs[id] = i;
            }

        }
        public List<string> Query(string key)
        {
            Dictionary<long, int> docCount = new Dictionary<long, int>();
            foreach (var item in key)
            {
                if (charIndexs.ContainsKey(item))
                {
                    int i = 1;
                    foreach (var ci in charIndexs[item])
                    {

                    }
                    i++;
                }
            }
            return (from q in docCount orderby q.Value descending select _data[q.Key]).ToList();
        }
        public List<string> Search(string param, string[] items)
        {
            var rs = new List<string>();

            var regex = new Regex(param);
            foreach (var item in items)
            {
                if (regex.IsMatch(item))
                {
                    rs.Add(item);
                }
            }
            return rs;
            //if (string.IsNullOrWhiteSpace(param) || items == null || items.Length == 0)
            //    return new string[0];

            //string[] words = param
            //                    .Split(new char[] { ' ', '\u3000' }, StringSplitOptions.RemoveEmptyEntries)
            //                    .OrderBy(s => s.Length)
            //                    .ToArray();

            //var q = from sentence in items.AsParallel()
            //        let MLL = Mul_LnCS_Length(sentence, words)
            //        where MLL >= 0
            //        orderby (MLL + 0.5) / sentence.Length, sentence
            //        select sentence;

            //return q.ToArray();
        }
        int Mul_LnCS_Length(string sentence, string[] words)
        {
            int sLength = sentence.Length;
            int result = sLength;
            bool[] flags = new bool[sLength];
            int[,] C = new int[sLength + 1, words[words.Length - 1].Length + 1];
            //int[,] C = new int[sLength + 1, words.Select(s => s.Length).Max() + 1];
            foreach (string word in words)
            {
                int wLength = word.Length;
                int first = 0, last = 0;
                int i = 0, j = 0, LCS_L;
                //foreach 速度会有所提升，还可以加剪枝
                for (i = 0; i < sLength; i++)
                    for (j = 0; j < wLength; j++)
                        if (sentence[i] == word[j])
                        {
                            C[i + 1, j + 1] = C[i, j] + 1;
                            if (first < C[i, j])
                            {
                                last = i;
                                first = C[i, j];
                            }
                        }
                        else
                            C[i + 1, j + 1] = Math.Max(C[i, j + 1], C[i + 1, j]);

                LCS_L = C[i, j];
                if (LCS_L <= wLength >> 1)
                    return -1;

                while (i > 0 && j > 0)
                {
                    if (C[i - 1, j - 1] + 1 == C[i, j])
                    {
                        i--;
                        j--;
                        if (!flags[i])
                        {
                            flags[i] = true;
                            result--;
                        }
                        first = i;
                    }
                    else if (C[i - 1, j] == C[i, j])
                        i--;
                    else// if (C[i, j - 1] == C[i, j])
                        j--;
                }

                if (LCS_L <= (last - first + 1) >> 1)
                    return -1;
            }

            return result;
        }

    }

    /// <summary>
    /// 字符索引
    /// </summary>
    public struct CharIndex
    {
        /// <summary>
        /// 文档编号
        /// </summary>
        public long DocID;
        /// <summary>
        /// 字符在文档出现的位置
        /// </summary>
        public List<int> Index;
    }
}
